package com.rivulet.foreign.controller;

import com.rivulet.foreign.config.UsernamePasswordIdentityToken;
import com.rivulet.foreign.pojo.User;
import com.rivulet.foreign.pojo.UserInformation;
import com.rivulet.foreign.service.ToolService;
import com.rivulet.foreign.service.UserService;
import com.rivulet.foreign.tools.Lock;
import com.rivulet.foreign.tools.Results;
import com.rivulet.foreign.tools.TimeTool;
import org.apache.poi.hssf.usermodel.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static com.rivulet.foreign.tools.Identity.*;


/**
 * {用户管理}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */

@Controller
public class UserController {

    final
    UserService userService;

    final
    ToolService authorizationForIdentity;

    public UserController(UserService userService, ToolService authorizationForIdentity) {
        this.userService = userService;
        this.authorizationForIdentity = authorizationForIdentity;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView loginPage(
            @RequestParam("user_id") String user_id,
            @RequestParam("password") String password,
            @RequestParam("identity") Integer identity,
            HttpServletResponse response
    ) {
        HashMap<String, String> map = new HashMap<>();
        User user = new User(user_id, password, identity);

        try {
            UsernamePasswordIdentityToken token = new UsernamePasswordIdentityToken(user);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            Cookie cookie = new Cookie("user_id", Lock.lockOn(user_id));
            response.addCookie(cookie);
            ModelAndView modelAndView = new ModelAndView("/login");
            if (identity.equals(ROOT)) {
                map.put("user_id", String.valueOf(user_id));
                map.put("message", "102");
                modelAndView.addAllObjects(map);
                return modelAndView;
            }
            if (identity.equals(TEACHER)) {
                map.put("user_id", String.valueOf(user_id));
                map.put("message", "101");
                modelAndView.addAllObjects(map);
                return modelAndView;
            }
            if (identity.equals(STUDENT)){
                map.put("user_id", String.valueOf(user_id));
                map.put("message", "100");
                modelAndView.addAllObjects(map);
                return modelAndView;
            }
            Exception e=new Exception("登录");
            return null;
        } catch (Exception e) {
            ModelAndView modelAndView = new ModelAndView("/login");
            map.put("message", "400");
            modelAndView.addAllObjects(map);
            return modelAndView;
        }
    }

    @RequestMapping(value = "/reSetPassword")
    @ResponseBody
    public Results rePassword(@RequestBody HashMap<String, String> map, @CookieValue(value = "user_id") String user_id) {
        User user = new User();
        user.setUser_id(Lock.lockOff(user_id));
        user.setPassword(map.get("old_password"));
        Results<Object> results = userService.reSetPassword(user, map.get("new_password"));
        return results;
    }
}
